package com.hex.ds.hdtp.core.web.sdm.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.sdm.dto.request.SdmConvertTaskRequestDto;
import com.hex.ds.hdtp.core.app.sdm.service.ISdmConvertTaskService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.sdm.converter.ISdmConvertTaskVoConverter;
import com.hex.ds.hdtp.core.web.sdm.vo.SdmConvertTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.nio.file.Files;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "定制化-SDM转换：任务")
@RequestMapping("/api/sdm/convert/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SdmConvertTaskController {

    private final ISdmConvertTaskService sdmConvertTaskService;
    private final ISdmConvertTaskVoConverter converter;
    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;


    @Log("上传SDM文档")
    @PostMapping(value = "/upload")
    @ApiOperation("上传SDM文档")
    public ResponseEntity upload(HttpServletRequest httpServletRequest) {
        log.info("进入【SdmConvertTaskController.upload】方法");
        List<MultipartFile> multipartFileList = ((MultipartHttpServletRequest) httpServletRequest).getFiles("fileList");
        return ResponseEntity.ok(sdmConvertTaskService.upload(multipartFileList));
    }

    @Log("新增SDM转换任务")
    @PostMapping
    @ApiOperation("新增SDM转换任务")
    public ResponseEntity add(@RequestBody SdmConvertTaskVo sdmConvertTaskVo) {
        log.info("进入【SdmConvertTaskController.add】方法: 参数: " + sdmConvertTaskVo.toString());
        return ResponseEntity.ok(sdmConvertTaskService.createTask(converter.toSdmConvertTaskRequestDto(sdmConvertTaskVo)));
    }

    @Log("SDM转换任务-分页查询")
    @GetMapping
    @ApiOperation("SDM转换任务-分页查询")
    public ResponseEntity queryListByPage(SdmConvertTaskVo sdmConvertTaskVo, PageVo pageVo) {
        log.debug("进入【SdmConvertTaskController.queryListByPage】方法,参数：{}", sdmConvertTaskVo.toString());
        SdmConvertTaskRequestDto requestDto = converter.toSdmConvertTaskRequestDto(sdmConvertTaskVo);
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        return ResponseEntity.ok(sdmConvertTaskService.queryListByPage(requestDto, pageDto));
    }

    @Log("下载文件文件")
    @GetMapping(value = {"/download/{fileType}/{pkId}"})
    @ApiOperation("下载文件")
    public ResponseEntity downloadFile(@PathVariable("fileType") String fileType, @PathVariable("pkId") final String pkId) throws Exception {
        log.info("进入【SdmConvertTaskController.downloadFile】方法,文件类型：{}，pkId：{}", fileType, pkId);
        String zipFilePath = sdmConvertTaskService.downloadFile(fileType, pkId);
        byte[] bytes = Files.readAllBytes(new File(zipFilePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=test2.zip")
                .body(bar);
    }
}
